package user

import (
	"day12_11_homework/internal/models/response"
	"day12_11_homework/pkg"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

type userPostLogin struct {
	Tel      string `json:"tel" binding:"required,ModelValidate"`
	Password string `json:"password" binding:"required"`
}

func UserLogin(c *gin.Context) {

	var userPosts userPostLogin

	if err := c.ShouldBindJSON(&userPosts); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	loginResponse, s := response.UserLoginResponse(userPosts.Tel, userPosts.Password)
	if s != "" {
		c.JSON(500, gin.H{
			"message": s,
		})
		return
	}

	handler, _ := pkg.TokenHandler(strconv.Itoa(loginResponse))

	c.JSON(200, gin.H{
		"message": "登录成功",
		"data":    handler,
	})

}
